package com.xhlj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhlj.controller.request_body.CommentBody;
import com.xhlj.entity.Comment;
import com.xhlj.service.CommentService;
import com.xhlj.mapper.CommentMapper;
import com.xhlj.utils.RedisKey;
import com.xhlj.utils.Result;
import jakarta.annotation.Resource;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author dell
 * @description 针对表【comment】的数据库操作Service实现
 * @createDate 2024-03-14 15:27:43
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {
    /*
     * 获取某一个帖子下的所有评论
     * 评论
     *   子评论
     * */
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getCommentList(Integer postId) {
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(Comment::getPostId, postId).isNull(Comment::getReplyId);
        List<Comment> parentList = commentMapper.selectList(commentWrapper);
        for (Comment item : parentList) {
            LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Comment::getReplyId, item.getCommentId());
            List<Comment> comments = commentMapper.selectList(wrapper);
            item.setReplyList(comments);

        }
        return Result.ok(parentList);
    }

    private void selectCommentList(Integer postId) {
    }

    /*
     * @param userId
     * @param commentId
     * */
    @Override
    public Result likeComment(Integer userId, String id) {
        Integer commentId = Integer.parseInt(id);
        /*错了我只需要管Commentid就行,至于谁点赞没用*/
        String key = RedisKey.Like_prefix+commentId;
        /*hasKey
         * hasKey中的key必须要有,不然直接报错
         * todo:登录的时候就给他加上  ops.putIfAbsent()
         *  现在直接用try-catch过了算了,不用了
         *
         * */
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if (stringRedisTemplate.hasKey(key)) {
            String value= ops.get(key);
            ops.set(key, Objects.equals(value, "1") ?"0":"1");
            if (value.equals("1")) {
                LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Comment::getCommentId, commentId);
                int before = commentMapper.selectOne(wrapper).getLiked();
                LambdaUpdateWrapper<Comment> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Comment::getCommentId, commentId)
                        .set(Comment::getLiked, before - 1);
                commentMapper.update(updateWrapper);
                return Result.build(null, 200, "取消点赞成功");
            } else {
                LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Comment::getCommentId, commentId);
                int before = commentMapper.selectOne(wrapper).getLiked();
                LambdaUpdateWrapper<Comment> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Comment::getCommentId, commentId)
                        .set(Comment::getLiked, before + 1);
                commentMapper.update(updateWrapper);
                return Result.build(null, 200, "点赞成功");
            }

        }else {
            ops.set(key, "1");
            LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Comment::getCommentId, commentId);
            int before = commentMapper.selectOne(wrapper).getLiked();
            LambdaUpdateWrapper<Comment> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Comment::getCommentId, commentId)
                    .set(Comment::getLiked, before + 1);
            commentMapper.update(updateWrapper);
            return Result.build(null, 200, "点赞成功");
        }
    }

    @Override
    public Comment addComment(CommentBody comment) {
        Comment comment1 = new Comment();
        comment1.setPostId(comment.getPostId());
        comment1.setContent(comment.getComment());
        comment1.setUserId(comment.getUserId());
        comment1.setLiked(0);
        comment1.setCreatedTime(new Date());
        int insert = commentMapper.insert(comment1);
        /*好像默认是会主键回显的*/
        return comment1;
    }
}




